package com.zhkj.txg.module.order.adapter

import android.widget.EditText
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.zhkj.txg.R
import com.zhkj.txg.module.order.entity.OrderGoodsEntity
import com.zhkj.txg.utils.ImageUtils
import com.zhkj.txg.utils.TextWatcherWrapper

class OrderGroupAdapter(
    private val onDescChange: (uiPosition: Int, content: String?) -> Unit
) :
    BaseQuickAdapter<OrderGoodsEntity, OrderShopViewHolder>(R.layout.item_order_group) {

    init {
        addChildClickViewIds(R.id.viewSelectShopCoupon)
    }

    override fun convert(helper: OrderShopViewHolder, item: OrderGoodsEntity) {

        val imageView = helper.setText(R.id.tvOrderGoodsName, item.goodsName)
            .setVisible(R.id.frame_shop, false)
            .setText(R.id.tvShopTotalFreight, if (item.isFreeFreight) "包邮" else "￥${item.freight}")
            .setText(R.id.tvOrderShopGoodsCount, "共${item.goodsNumber}件，小计")
            .setText(
                R.id.tvShopCouponName,
                if (item.selectCoupon == null) "" else item.selectCoupon!!.name
            )
            .setText(R.id.tvOrderShopAmount, "￥${item.useCouponTotalPrice}")
            .setText(R.id.tvOrderGoodsPrice, "￥${item.goodsPrice}")
            .setText(R.id.tvOrderGoodsNumber, "x${item.goodsNumber}")
            .getView<ImageView>(R.id.ivOrderGoods)
        helper.getView<EditText>(R.id.tvOrderGoodsDesc).addTextChangedListener(TextWatcherWrapper {
            onDescChange(helper.layoutPosition, it?.toString())
        })

        ImageUtils.loadRoundCornerImage(context, imageView, item.image)
    }
}